package helper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class ReadConfig {

    public Connection getDBConnection() {
        try {
            Properties configFile = new Properties();
            configFile.load(this.getClass().getResourceAsStream("/config/config.properties"));

            String DB_IP = configFile.getProperty("DB_IP");
            String DB_Port = configFile.getProperty("DB_Port");
            String DB_InstanceName = configFile.getProperty("DB_InstanceName");
            String DB_Name = configFile.getProperty("DB_Name");
            String DB_User = configFile.getProperty("DB_User");
            String DB_Pass = configFile.getProperty("DB_Pass");

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://" + DB_IP + ":" + DB_Port + ";instance=" + DB_InstanceName
                    + ";databaseName=" + DB_Name;
            Connection conn = DriverManager.getConnection(url, DB_User, DB_Pass);
            return conn;
        } catch (Exception e) {
            return null;
        }
    }

    public String getDefaultValue(String default_name) {
        try {
            Properties prop = new Properties();
            prop.load(this.getClass().getResourceAsStream("config/config.properties"));
            String result = prop.getProperty(default_name);
            return result;
        } catch (Exception e) {
            return null;
        }
    }

}
